package com.yimi.policyservice.service.impl;

import com.github.pagehelper.Page;
import com.yimi.policyservice.dao.system.SysRoleMapperCustom;
import com.yimi.policyservice.dto.policyaddr.IdsBean;
import com.yimi.policyservice.dto.role.*;
import com.yimi.policyservice.entity.base.system.*;
import com.yimi.policyservice.repository.*;
import com.yimi.policyservice.service.RoleService;
import com.yimi.policyservice.utils.BeanHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jack
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private SysRoleRepository sysRoleRepository;

    @Autowired
    private SysRoleMapperCustom sysRoleMapperCustom;

    @Autowired
    private SysUserRoleRepsitory sysUserRoleRepsitory ;

    @Autowired
    private SysMenuRepository sysMenuRepository ;

    @Autowired
    private SysRolePermissionRepository sysRolePermissionRepository ;

    @Autowired
    private SysPermissionRepository sysPermissionRepository ;

    @Autowired
    private UserDataPermissionRepository userDataPermissionRepository ;

    @Override
    public void delete(String id) {
        sysRoleRepository.deleteById(id);
        sysUserRoleRepsitory.deleteByRoleId(id);


    }

    @Override
    public void update(RoleDto bean, String id) {
        SysRole sysRole = sysRoleRepository.findById(id).get();
        BeanHelper.copyProperties(bean,sysRole);
        sysRoleRepository.save(sysRole);
    }

    @Override
    public SysRole4Get create(RoleDto bean) {
        SysRole role = new SysRole();
        BeanUtils.copyProperties(bean,role);
        role.setCreateDate(new Date());
        sysRoleRepository.save(role);
        SysRole4Get get = new SysRole4Get();
        BeanUtils.copyProperties(role,get);
        return get;
    }

    @Override
    public List<SysRole4Get> findAll() {
        List<SysRole> roles =  sysRoleRepository.findAllOrderByCreateDateDesc();
        return BeanHelper.convertList(roles,SysRole.class,SysRole4Get.class);
    }

    @Override
    public Page<SysRole4Get> search(String key) {
        return sysRoleMapperCustom.search(key);
    }

    @Override
    public MenuTreeDto getMenuTree() {
        MenuTreeDto menuTreeDto = new MenuTreeDto();
        menuTreeDto.setId("0");
        menuTreeDto.setMenuName("导航");
        append(menuTreeDto);
        return  menuTreeDto ;
    }


    @Override
    public void setPermission(String id, List<String> ids,int type) {
        sysRolePermissionRepository.deleteByRoleId(id,type);
        List<SysRolePermission> ps =new ArrayList<>();
        ids.forEach(targetId->{
            SysRolePermission p = new SysRolePermission();
            if(type==0){
                p.setMenuId(targetId);
            }else{
                p.setPermissionId(targetId);
            }
            p.setRoleId(id);
            p.setType(type);
            ps.add(p);
        });
        if(!ps.isEmpty()){
            sysRolePermissionRepository.saveAll(ps);
        }
    }


    @Override
    public IdsBean getRoleMenuIds(String id) {
        List<String> ids = sysRolePermissionRepository.findMenuIdByTypeAndRoleId(0,id);
        return  new IdsBean(ids);
    }


    @Override
    public List<MenuTreeDto> getPagePermisssion() {
        List<SysMenu> list = sysMenuRepository.findByMenuLevel(2L);
        List<MenuTreeDto> res = BeanHelper.convertList(list,SysMenu.class,MenuTreeDto.class);
        for(MenuTreeDto m : res){
            List<SysPermission> ps = sysPermissionRepository.findByMenuId(m.getId());
            List<MenuTreeDto> buffer = new ArrayList<>();
            ps.forEach(p->{
                MenuTreeDto dto = new MenuTreeDto();
                dto.setId(p.getId());
                dto.setMenuName(p.getName());
                buffer.add(dto);
            });
            m.setChildren(buffer);
        }
        return res ;
    }

    @Override
    public IdsBean getRolePages(String id) {
        List<String> ids = sysRolePermissionRepository.findPermissionIdByTypeAndRoleId(1,id);
        return  new IdsBean(ids);
    }

    @Override
    public SysRole4Get findOne(String id) {
        SysRole sysRole = sysRoleRepository.findById(id).get();
        SysRole4Get get = new SysRole4Get();
        BeanUtils.copyProperties(sysRole,get);
        return get;
    }


    @Override
    public void addUserDataRole(List<DataRoleDto> roles, SysUser createUser,String userId) {
        userDataPermissionRepository.deleteByUserId(userId);
        List<SysUserDataPermission> datas = BeanHelper.convertList(roles,DataRoleDto.class,SysUserDataPermission.class);
        datas.forEach(item ->{
            item.setUserId(userId);
            item.setCreateId(createUser.getId());
            item.setCreateTime(new Date());
        });
        userDataPermissionRepository.saveAll(datas);
     }


    @Override
    public List<String> getUserDataPermissionByType(String userId, int type) {
        return userDataPermissionRepository.findUserPermissions(userId,type);
    }

    private void append(MenuTreeDto tree){
        List<SysMenu> list = sysMenuRepository.findByParentMenuIdOrderByMenuSortAsc(tree.getId());
        List<MenuTreeDto> menuTreeDtos = new ArrayList<>();
        if(list.isEmpty()){
            return;
        }
        for(SysMenu m : list){
            MenuTreeDto d = new MenuTreeDto();
            BeanUtils.copyProperties(m,d);
            append(d);
            menuTreeDtos.add(d);
        }
        tree.setChildren(menuTreeDtos);
    }
}
